package com.example.Arithmetic.Lettcode;

import java.util.Arrays;

/**
 * 日期：2023/12/28
 * 时间：19:53
 * 描述：力扣1636题，按照频率将数组升序排序，次数相同则降序
 */
public class E01Lettcode1636 {
    public int[] frequencySort(int[] nums) {
        int[] ints = new int[201];
        for (int n : nums) {
            ints[n + 100]++;
        }
        return Arrays.stream(nums).boxed()
                .sorted(
                        (a, b) -> {
                            int af = ints[a + 100];
                            int bf = ints[b + 100];
                            if (af <bf) {
                                return -1;
                            } else if (af >bf) {
                                return 1;
                            }else {
                                return b-a;
                            }
                        }
                ).mapToInt(Integer::intValue).toArray();
    }

    public static void main(String[] args) {
        int[] ints=new int[]{1,1,2,2,2,3,3};
        E01Lettcode1636 e01Lettcode1636=new E01Lettcode1636();
        int[] ints1 = e01Lettcode1636.frequencySort(ints);
        System.out.println(Arrays.toString(ints1));
    }
}

